﻿using log4net;
using log4net.Config;
using LogText.EF;
using LogText.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace LogText.Controllers
{
    public class LogTestController:Controller
    {
        public ActionResult Index()
        {
            log4net.ILog log = log4net.LogManager.GetLogger("testApp.Logging");//获取一个日志记录器

            log.Info(DateTime.Now.ToString() + ": login success");//写入一条新log


            return View();
        }


        public void GetLog()
        {
            string ip= Request.UserHostAddress;
            string brower = Request.Browser.Browser + Request.Browser.Version;
            string classname = this.GetType().Name;
            string MachineName = Request.UserHostName;
            var logger = LogManager.GetLogger("mylog"); 
            logger.Info(new LogMessage(1, classname, 1,"去死吧", ip, MachineName, brower));
            logger.Warn(new LogMessage(1, classname, 1, "啦啦啦", ip, MachineName, brower));
            logger.Error(new LogMessage(1, classname, 1, "我的日志框架果然有问题", ip, MachineName, brower));
            logger.Fatal(new LogMessage(1, classname, 1, "我可能玩的是假日志框架", ip, MachineName, brower));
            logger.Debug(new LogMessage(1, classname, 1, "这只是测试信息而已", ip, MachineName, brower));
            LogTestModel mode = new LogTestModel();
            List<Log> lla=  mode.Log.OrderByDescending(o => o.Date).Skip(0).Take(5).ToList();
            foreach (var item in lla)
            {
                Response.Write("记录时间：" + item.Date + "<br/>线程ID：" + item.Thread + "<br/>日志级别：" + item.Level +
                         "<br/>记录类：" + item.Logger + "<br/>操作者id：" + item.Operator+"<br/>消息：" + item.Message +
                        "<br/>动作类型：" + item.ActionType + "<br/>操作对象：" + item.Operand + "<br/>IP地址：" + ip + "<br/>机器名称：" +
                        item.MachineName + "<br/>浏览器：" + item.Browser + "<br/>路径：" + item.Location + "<br/>异常：" + item.Exception);
                Response.Write("<br/>");
                Response.Write("<br/>");
            } 
        }
       

    }
}
